// gmx shines, thanks the great gmx
// cyx shines, thanks the great cyx
// freopen,froepen,ioi,akioi,ccf
// everyone plz, watch Arknights live
#include <bits/stdc++.h>
using namespace std;
constexpr int N(1e6);
using LL=long long;
LL a[N+5],s[N+5],f[N+5],t[N+5];
/*
f[i]=maxj s[i-1]-s[j]+f[j]
f[i]=maxj s[i-1]-s[j]+f[j]+A[i] (A[j-1]==A[i])
*/
int main() {
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin.tie(nullptr)->sync_with_stdio(false);
    int T;cin>>T;
    while (T--) {
        int n;cin>>n;
        fill_n(t+1,N,-1e18);
        LL pre{0};
        for (int i{1};i<=n;++i) {
            cin>>a[i];
            s[i]=s[i-1];
            if (a[i]==a[i-1]) s[i]+=a[i];
            f[i]=s[i-1]+pre;
            f[i]=max(f[i],s[i-1]+t[a[i]]+a[i]);
            pre=max(pre,f[i]-s[i]);
            t[a[i-1]]=max(t[a[i]],f[i]-s[i]);
        }
        LL ans{0};
        for (int i{1};i<=n;++i) ans=max(ans,s[n]-s[i]+f[i]);
        cout<<ans<<"\n";
    }
    return 0;
}
/*
1
8
3 5 2 5 1 2 1 4
*/